<template>
  <div class="home">
    <el-container>
      <el-aside width="auto">
        <div class="logo"></div>
        <el-menu
          default-active="2"
          router
          :collapse="isCollapse"
          class="el-menu-admin"
          background-color="#F9F9F9"
          @open="handleOpen"
          @close="handleClose"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>用户管理</span>
            </template>
            <!-- index会路由的path进行跳转，因为通过router属性开启了路由模式 -->
              <el-menu-item index="/users">
                <i class="el-icon-menu"></i>
                <span>用户管理</span>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-container>
        <el-header>
          <div class="toggle-btn">
            <i class="myicon myicon-menu" @click="toggle"></i>
          </div>
          <div class="system-title">电商后台管理系统</div>
          <div class="welcome">
            <span>您好，{{username}}</span>
            <el-button type="danger" round @click="logout">退出</el-button>
          </div>
        </el-header>
        <!-- 中间主体内容 -->
        <el-main>
          <!-- 用于子路由的组件 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  created () {
    this.username = localStorage.getItem('username')
  },
  data () {
    return {
      username: '',
      isCollapse: false
    }
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    toggle () {
      this.isCollapse = !this.isCollapse
    },
    logout () {
      // 清除localStorage值
      localStorage.clear()
      // 返回登陆页面
      this.$router.push({ name: '/login' })
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #f9f9f9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409eff;
  }
  .logo {
    height: 60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292cf;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome {
    color: white;
    span {
      margin-right: 15px;
    }
  }
}
</style>
